package com.compay4;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class ShoppingUI {
    static ShoppingService service = new ShoppingService();

    public static void main(String[] args) throws SQLException {

        while (true) {
            System.out.println("--------欢迎使用速食冻品系统--------");
            System.out.println("         1 添加商品信息            ");
            System.out.println("         2 删除商品信息            ");
            System.out.println("         3 修改商品信息            ");
            System.out.println("         4 查看商品信息            ");
            System.out.println("         5 退出系统               ");
            System.out.println("请输入您要选择的序号：");

            //2. 用Scanner实现键盘录入数据
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();

            switch (line) {
                case "1":
                    System.out.println("1 按编号添加商品信息");
                    System.out.println("2 按名称添加商品信息");
                    String figure1 = sc.nextLine();
                    if ("1".equals(figure1)) {
                        addNumber();
                    } else if ("2".equals(figure1)) {
                        addName();
                    }
                    break;
                case "2":
                    System.out.println("1 按编号删除商品信息");
                    System.out.println("2 按名称删除商品信息");
                    String figure2 = sc.nextLine();
                    if ("1".equals(figure2)) {
                        deleteNumber();
                    } else if ("2".equals(figure2)) {
                        deleteName();
                    }
                    break;
                case "3":
                    System.out.println("1 按编号修改商品信息");
                    System.out.println("2 按名称修改商品信息");
                    String figure3 = sc.nextLine();
                    if ("1".equals(figure3)) {
                        correctNumber();
                    } else if ("2".equals(figure3)) {
                        correctName();
                    }
                    break;
                case "4":
                    System.out.println("1 查看所有商品信息");
                    System.out.println("2 查看一个商品信息");
                    String figure = sc.nextLine();
                    switch (figure) {
                        case "1":
                            System.out.println("1 按编号查找");
                            System.out.println("2 按名称查找");
                            String fig = sc.nextLine();
                            if ("1".equals(fig)) {
                                findAllNumber();
                            } else if ("2".equals(fig)) {
                                findAllName();
                            }
                            break;
                        case "2":
                            System.out.println("1 按编号查找");
                            System.out.println("2 按名称查找");
                            String fig1 = sc.nextLine();
                            if ("1".equals(fig1)) {
                                findNumber();
                            } else if ("2".equals(fig1)) {
                                findName();
                            }
                            break;
                    }
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    System.exit(0);  //JVM退出
            }
        }

    }

    //按编号查找全部
    private static void findAllNumber() throws SQLException {

        ResultSet rs = service.findFromShoppingInfo();

        System.out.println("number\tname\tprice");
        while (rs.next()) {
            String num = rs.getString("number"); // 通过列名获取指定字段的值
            String name = rs.getString("name");
            String pri = rs.getString("price");

            System.out.println(num + "\t" + name + "\t\t" + pri);
        }

    }

    //按编号查找一个
    private static void findNumber() throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要查找的编号");
        String number = sc.nextLine();
        while (rs.next()) {
            if (rs.getString("number").equals(number)) {
                System.out.println("number\tname\tprice");
                String num = rs.getString("number");
                String name = rs.getString("name");
                String pri = rs.getString("price");
                System.out.println(num + "\t" + name + "\t\t" + pri);
                break;
            }

        }
    }

    //按名称查找全部
    private static void findAllName() throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();
        System.out.println("number\tname\tprice");
        while (rs.next()) {
            String num = rs.getString("number"); // 通过列名获取指定字段的值
            String name = rs.getString("name");
            String pri = rs.getString("price");

            System.out.println(num + "\t" + name + "\t\t" + pri);
        }

    }

    //按名称查找一个
    private static void findName() throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要查找的名称");
        String name1 = sc.nextLine();
        while (rs.next()) {
            if (rs.getString("name").equals(name1)) {
                System.out.println("number\tname\tprice");
                String num = rs.getString("number");
                String name = rs.getString("name");
                String pri = rs.getString("price");
                System.out.println(num + "\t" + name + "\t\t" + pri);
                break;
            }

        }
    }

    //判断编号是否被使用
    public static boolean isUsedNUmber(String number) throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();
        boolean flag = false;
        //判断是否有相同的编号
        while (rs.next()) {
            if (rs.getString("number").equals(number)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    //判断名称是否被使用
    public static boolean isUsedName(String name) throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();
        boolean flag = false;
        //判断是否有相同的编号
        while (rs.next()) {
            if (rs.getString("name").equals(name)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    //按编号添加
    private static void addNumber() throws SQLException {

        Scanner sc = new Scanner(System.in);
        String number1;
        while (true) {
            System.out.print("请输入你要添加的编号：");
            number1 = sc.nextLine();
            boolean flag = isUsedNUmber(number1);
            if (flag) {
                System.out.println("您输入的编号已存在，请重新输入");
            } else {
                break;
            }

        }
        String name1;
        while (true) {
            System.out.print("请输入你要添加的名字：");
            name1 = sc.nextLine();
            boolean flag = isUsedName(name1);
            if (flag) {
                System.out.println("您输入的名称已存在，请重新输入");
            } else {
                break;
            }

        }
        System.out.print("请输入你要添加的价格：");
        String price1 = sc.nextLine();

        //调用service
        service.addShopping(number1, name1, price1);
        System.out.println("添加成功");

    }

    //按名称添加
    private static void addName() throws SQLException {

        Scanner sc = new Scanner(System.in);
        String name1;
        while (true) {
            System.out.print("请输入你要添加的名称：");
            name1 = sc.nextLine();
            boolean flag = isUsedName(name1);
            if (flag) {
                System.out.println("您输入的名称已存在，请重新输入");
            } else {
                break;
            }

        }
        String number1;
        while (true) {
            System.out.print("请输入你要添加的编号：");
            number1 = sc.nextLine();
            boolean flag = isUsedNUmber(number1);
            if (flag) {
                System.out.println("您输入的编号已存在，请重新输入");
            } else {
                break;
            }

        }

        System.out.print("请输入你要添加的价格：");
        String price1 = sc.nextLine();

        //调用service
        service.addShopping(number1, name1, price1);
        System.out.println("添加成功");

    }

    //按编号删除
    private static void deleteNumber() throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要删除的商品编号：");
        String number2 = sc.nextLine();
        boolean flag = false;
        while (rs.next()) {
            if (rs.getString("number").equals(number2)) {
                service.deleteShoppingNumber(number2);
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("删除成功");
        } else {
            System.out.println("该信息不存在");
        }

    }

    //按名称删除
    private static void deleteName() throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入要删除的商品名称：");
        String name2 = sc.nextLine();
        boolean flag = false;
        while (rs.next()) {
            if (rs.getString("name").equals(name2)) {
                service.deleteShoppingName(name2);
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("删除成功");
        } else {
            System.out.println("该信息不存在");
        }

    }

    //按编号修改
    private static void correctNumber() throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要修改的商品编号：");
        String number3 = sc.nextLine();

        boolean flag = false;
        while (rs.next()) {
            if (rs.getString("number").equals(number3)) {
                String number = number3;
                System.out.print("请输入新价格：");
                String price2 = sc.nextLine();
                service.correctShoppingNumber(price2, number);
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("修改成功");
        } else {
            System.out.println("该信息不存在");
        }

    }

    //按名称修改
    private static void correctName() throws SQLException {
        ResultSet rs = service.findFromShoppingInfo();
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入要修改的商品名称：");
        String name3 = sc.nextLine();

        boolean flag = false;
        while (rs.next()) {
            if (rs.getString("name").equals(name3)) {
                String name = name3;
                System.out.print("请输入新价格：");
                String price3 = sc.nextLine();
                service.correctShoppingName(price3, name);
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("修改成功");
        } else {
            System.out.println("该信息不存在");
        }

    }


}
